package com.iat.demo.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 音频文件读取工具类
 */
public class AudioUtils {

    /**
     * 读取音频文件内容
     *
     * @param filePath 文件路径
     * @return 音频字节数据
     * @throws IOException 读取异常
     */
    public static byte[] readAudioFile(String filePath) throws IOException {
        File audioFile = new File(filePath);
        if (!audioFile.exists()) {
            throw new IOException("Audio file not found: " + filePath);
        }
        
        return Files.readAllBytes(Paths.get(filePath));
    }
    
    /**
     * 分片读取音频文件
     *
     * @param filePath 文件路径
     * @param chunkSize 分片大小（字节数）
     * @return 分片数组
     * @throws IOException 读取异常
     */
    public static byte[][] readAudioFileByChunk(String filePath, int chunkSize) throws IOException {
        byte[] audioData = readAudioFile(filePath);
        int chunkCount = (int) Math.ceil((double) audioData.length / chunkSize);
        byte[][] chunks = new byte[chunkCount][];
        
        for (int i = 0; i < chunkCount; i++) {
            int start = i * chunkSize;
            int end = Math.min(start + chunkSize, audioData.length);
            int length = end - start;
            
            chunks[i] = new byte[length];
            System.arraycopy(audioData, start, chunks[i], 0, length);
        }
        
        return chunks;
    }
} 